package com.supply.dl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.BindView;

import com.supply.dl.R;
import com.supply.dl.app.AppConfig;
import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.bean.JobInfoBean;
import com.supply.dl.bean.PositionInfoBean;
import com.supply.dl.bean.SearchStockBean;
import com.supply.dl.bean.SelectDictionaryBean;
import com.supply.dl.bean.request.AttriChangeRequest;
import com.supply.dl.http.HttpManger;

/**
 * 属性转移
 */
public class AttributeChangeActivity extends BaseActivity {
    private SelectDictionaryBean beforeBeanCu, afterBeanCu, afterType,
            afterStatus, wuLiaoBean, selectBean,
            departBean, personBean;
    private SearchStockBean beforeBeanCuLbs, afterBeanCuLbs;
    public static final int BEFORE_CK = 101;
    public static final int AFTER_CK = 102;
    public static final int BEFORE_CK_LBS = 103;
    public static final int AFTER_CK_LBS = 104;
    public static final int SEARCH_WL_KEY = 106;  //物料

    @BindView(R.id.layout_after)
    LinearLayout layoutAfter;
    @BindView(R.id.txt_cu)
    TextView txtCu;
    @BindView(R.id.txt_kw)
    TextView txtKw;
    @BindView(R.id.txt_wu_info)
    TextView txtWinfo;

//    @BindView(R.id.txt_after_cu)
//    TextView txtAfterCu;
//    @BindView(R.id.txt_after_kw)
//    TextView txtAfterKw;
    @BindView(R.id.edit_num_jian) //件
    EditText txtJian;
    @BindView(R.id.edit_num_dun) //吨
    EditText txtDun;
    @BindView(R.id.edit_num_fang) //方
    EditText txtFang;
//    @BindView(R.id.txt_kucun_type)
//    TextView txtType;
//    @BindView(R.id.txt_kucun_status)
//    TextView txtStatus;
//    @BindView(R.id.txt_picihao)
//    EditText txtPch;
//    @BindView(R.id.edit_why)
//    EditText editWhy;
    @BindView(R.id.item1)
    TextView item1; // 物料编码
    @BindView(R.id.item2)
    TextView item2; // 物料名称
    @BindView(R.id.item3)
    TextView item3; //库存状态
    @BindView(R.id.item4)
    TextView item4; //库存类型
    @BindView(R.id.item5)
    TextView item5; //批次号
    @BindView(R.id.item6)
    TextView item6; //规格

    @BindView(R.id.item7)
    EditText item7; //单件重量
    @BindView(R.id.item8)
    EditText item8; //备注
    @BindView(R.id.item9)
    EditText item9; // 转移原因
    @BindView(R.id.item10)
    EditText item10; //关联单号
    @BindView(R.id.item11)
    TextView item11; //责任单位
    @BindView(R.id.item12)
    TextView item12; //责任人


    @BindView(R.id.txt_save)
    TextView txtSave;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_attribute_change;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleShow(getResourcesString(R.string.attribute_transfer));
        showLeftBack();
        txtCu.setOnClickListener(this);
        txtKw.setOnClickListener(this);
        item4.setOnClickListener(this);
        item3.setOnClickListener(this);
        txtSave.setOnClickListener(this);
        item1.setOnClickListener(this);
        item6.setOnClickListener(this);
        item11.setOnClickListener(this);
        item12.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == txtKw) {
            if (beforeBeanCu == null) {
                showToast("请先选择仓库");
                return;
            }
            Intent intent = new Intent(this, DepotSelectActivity.class);
            intent.putExtra("whId", beforeBeanCu.getValue());
            startActivityForResult(intent, BEFORE_CK_LBS);
        } else if (v == txtCu) {
            Intent intent = new Intent(this, SelectBaseDataActivity.class);
            intent.putExtra("action", AppConfig.BaseDataAction.WARE_SELECT_WAREHOUSE);
            startActivityForResult(intent, BEFORE_CK);
        }else if(v == item1){ //物料编码
            Intent intent1 = new Intent(this, MaterialSelectActivity.class);
            startActivityForResult(intent1, SEARCH_WL_KEY);
        }else if(v == item6){ //规格
            Intent intent = new Intent(this, SelectBaseDataActivity.class);
            intent.putExtra("action", AppConfig.BaseDataAction.WARE_SELECT_SKU_UNIT);
            intent.putExtra("skuId", beforeBeanCuLbs.getSkuId());   //物料ID
            startActivityForResult(intent, AppConfig.BaseDataAction.REQCODE_SKU_UNIT);
        }
        else if (v == item4) {
            Intent intent = new Intent(this, SelectBaseDataActivity.class);
            intent.putExtra("action", AppConfig.BaseDataAction.STO_INVENTORY_STOCK_TYPE);
            startActivityForResult(intent, AppConfig.BaseDataAction.REQCODE_STO_INVENTORY_STOCK_TYPE);
        } else if (v == item3) {
            Intent intent = new Intent(this, SelectBaseDataActivity.class);
            intent.putExtra("action", AppConfig.BaseDataAction.STO_INVENTORY_STOCK_STATUS);
            startActivityForResult(intent, AppConfig.BaseDataAction.REQCODE_STO_INVENTORY_STOCK_STATUS);
        }else if(v == item11){ //责任单位
            Intent intent = new Intent(this, SelectBaseDataActivity.class);
            intent.putExtra("action", AppConfig.BaseDataAction.STO_DEPART);
            startActivityForResult(intent, AppConfig.BaseDataAction.REQCODE_STO_DEPART);
        }else if(v == item12){ //责任人
            if (departBean == null) {
                showToast("请先选择责任单位");
                return;
            }
            Intent intent = new Intent(this, SelectBaseDataActivity.class);
            intent.putExtra("action", AppConfig.BaseDataAction.STO_PERSON);
            intent.putExtra("departId", departBean.getValue());   //物料ID
            startActivityForResult(intent, AppConfig.BaseDataAction.REQCODE_STO_PERSON);
        }
        else if (v == txtSave) {
            saveAttriChangeInfo();
        }
    }

    /**
     * 保存属性转移
     */
    private void saveAttriChangeInfo() {
        HttpManger httpManger = HttpManger.getInstance();
        httpManger.setContext(this);

        AttriChangeRequest attriChangeRequest = new AttriChangeRequest();
        attriChangeRequest.setInvId(beforeBeanCuLbs.getId());
        attriChangeRequest.setAmount(beforeBeanCuLbs.getAmount() + "");

        attriChangeRequest.setTraSkuId(wuLiaoBean.getValue());
        attriChangeRequest.setTraSkuCode(wuLiaoBean.getCode());
        attriChangeRequest.setTraSkuName(wuLiaoBean.getText());
        attriChangeRequest.setTraAmount(txtJian.getText().toString().trim());
        attriChangeRequest.setTraWeight(txtDun.getText().toString().trim());
        attriChangeRequest.setTraVolume(txtFang.getText().toString().trim());
        attriChangeRequest.setTraStockStatus(afterStatus.getValue());
        attriChangeRequest.setTraStockType(afterType.getValue());
        attriChangeRequest.setTraBatchNo(item5.getText().toString().trim());
        attriChangeRequest.setTraUnitId(selectBean.getValue());
        attriChangeRequest.setPackageWeight(item7.getText().toString().trim());
        attriChangeRequest.setTraUnitId(selectBean.getValue()); //规格
        //关联单号
        attriChangeRequest.setAssociatedNo(item10.getText().toString().trim());
        attriChangeRequest.setTraRemark(item9.getText().toString().trim());
        //责任单位和人
        if(departBean != null){
            attriChangeRequest.setDutyDeptId(departBean.getValue());
            attriChangeRequest.setDutyDeptName(departBean.getText());
        }
        if(personBean != null){
            attriChangeRequest.setDutyUserId(personBean.getValue());
            attriChangeRequest.setDutyUserName(personBean.getText());
        }


//        attriChangeRequest.setDutyDeptId(beforeBeanCuLbs.getSupplierId());  // TODO: 应该传什么数据？
//        attriChangeRequest.setDutyDeptName(beforeBeanCuLbs.getSupplierName());

        httpManger.saveAttriChange(attriChangeRequest);
        httpManger.setHttpResponseCallBackListener(new HttpManger.HttpResponseCallBackListener() {
            @Override
            public void onSuccess(BaseInfo<?> baseInfo) {
                showToast(baseInfo.getMessage());
                finish();
            }

            @Override
            public void onFailure(String message) {
                showToast(message);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            switch (requestCode) {
                case SEARCH_WL_KEY: //物料编码
                    wuLiaoBean = (SelectDictionaryBean) data.getSerializableExtra("selectBean");
                    if (wuLiaoBean == null)
                        return;
                    item1.setText(wuLiaoBean.getCode());
                    item2.setText(wuLiaoBean.getText());


                    break;
                case AppConfig.BaseDataAction.REQCODE_SKU_UNIT://规格
                    selectBean= (SelectDictionaryBean) data.getSerializableExtra("selectBean");
                    if(selectBean == null)
                        return;
                    item6.setText(selectBean.getText());


                    break;
                case BEFORE_CK:
                    beforeBeanCu = (SelectDictionaryBean) data.getSerializableExtra("selectBean");
                    if (beforeBeanCu == null)
                        return;
                    txtCu.setText(beforeBeanCu.getText());
//                    txtAfterCu.setText(beforeBeanCu.getText());
                    break;
                case BEFORE_CK_LBS: //库位
                    beforeBeanCuLbs = (SearchStockBean) data.getSerializableExtra("selectBean");
                    if (beforeBeanCuLbs == null) {
                        layoutAfter.setVisibility(View.GONE);
                        return;
                    }
                    layoutAfter.setVisibility(View.VISIBLE);
                    txtKw.setText(beforeBeanCuLbs.getStockName());
//                    txtAfterKw.setText(beforeBeanCuLbs.getStockName());
                    String wulInfo = "物料编码：" + beforeBeanCuLbs.getSkuCode() + "\n" + "物料名称：" + beforeBeanCuLbs.getSkuName() + "\n"
                            + "库存类型：" + beforeBeanCuLbs.getStockTypeName() + "\n" + "库存状态：" + beforeBeanCuLbs.getStockStatusName() + "\n"
                            + "批次号：" + beforeBeanCuLbs.getBatchNo() + "\n" + "数量(件)：" + beforeBeanCuLbs.getAmount() + "\n"
                            + "数量(吨)：" + beforeBeanCuLbs.getWeight() + "\n"+ "数量(方)：" + beforeBeanCuLbs.getVolume() + "\n"
                            + "规格：" + beforeBeanCuLbs.getUnitName() + "\n" + "单件重量：" + beforeBeanCuLbs.getPackageWeight() + "\n"
                            + "备注：" + beforeBeanCuLbs.getRemark();
                    txtWinfo.setText(wulInfo);

                    txtJian.setText(beforeBeanCuLbs.getAmount());
                    txtDun.setText(beforeBeanCuLbs.getWeight());
                    txtFang.setText(beforeBeanCuLbs.getVolume());
                    //单件重量
                    item7.setText(beforeBeanCuLbs.getPackageWeight());
                    item8.setText(beforeBeanCuLbs.getRemark());


                    selectBean = new SelectDictionaryBean(beforeBeanCuLbs.getUnitName(),"");
                    selectBean.setValue(beforeBeanCuLbs.getUnitId());
                    item6.setText(selectBean.getText()); //规格
                    item5.setText(beforeBeanCuLbs.getBatchNo());
                    item4.setText(beforeBeanCuLbs.getStockTypeName());
                    afterType = new SelectDictionaryBean(beforeBeanCuLbs.getStockTypeName(),"");
                    afterType.setValue(beforeBeanCuLbs.getStockType());
                    item3.setText(beforeBeanCuLbs.getStockStatusName());
                    afterStatus = new SelectDictionaryBean(beforeBeanCuLbs.getStockStatusName(),"");
                    afterStatus.setValue(beforeBeanCuLbs.getStockStatus());
                    //显示物料编码
                    wuLiaoBean = new SelectDictionaryBean(beforeBeanCuLbs.getSkuName(), beforeBeanCuLbs.getSkuCode());
                    wuLiaoBean.setValue(beforeBeanCuLbs.getSkuId());
                    item1.setText(wuLiaoBean.getCode());
                    item2.setText(wuLiaoBean.getText());

                    break;
                case AFTER_CK:
                    afterBeanCu = (SelectDictionaryBean) data.getSerializableExtra("selectBean");
                    if (afterBeanCu == null)
                        return;
//                    txtAfterCu.setText(afterBeanCu.getText());
                    break;
                case AFTER_CK_LBS:
                    afterBeanCuLbs = (SearchStockBean) data.getSerializableExtra("selectBean");
                    if (afterBeanCuLbs == null)
                        return;
//                    txtAfterKw.setText(afterBeanCuLbs.getWhName());
                    break;
                case AppConfig.BaseDataAction.REQCODE_STO_INVENTORY_STOCK_TYPE:
                    afterType = (SelectDictionaryBean) data.getSerializableExtra("selectBean");
                    if (afterType == null)
                        return;
                    item4.setText(afterType.getText());
                    break;
                case AppConfig.BaseDataAction.REQCODE_STO_INVENTORY_STOCK_STATUS:
                    afterStatus = (SelectDictionaryBean) data.getSerializableExtra("selectBean");
                    if (afterStatus == null)
                        return;
                    item3.setText(afterStatus.getText());
                    break;
                case AppConfig.BaseDataAction.REQCODE_STO_DEPART: //责任单位
                    departBean = (SelectDictionaryBean) data.getSerializableExtra("selectBean");
                    if (departBean == null)
                        return;
                    item11.setText(departBean.getText());
                    break;
                case AppConfig.BaseDataAction.REQCODE_STO_PERSON: //责任人
                    personBean = (SelectDictionaryBean) data.getSerializableExtra("selectBean");
                    if (personBean == null)
                        return;
                    item12.setText(personBean.getText());
                    break;
            }
        }
    }
}
